import wx

from core.task import Task


class OrderByPanel(wx.Panel):
    def __init__(self, parent, task: Task):
        super().__init__(parent)
        self.task = task
        self.prevent_update = False

        # 主布局
        main_sizer = wx.BoxSizer(wx.VERTICAL)

        # 启用复选框
        self.enable_check = wx.CheckBox(self, label="启用排序")
        main_sizer.Add(self.enable_check, 0, wx.BOTTOM, 5)

        # 排序设置GroupBox
        group_box = wx.StaticBox(self, label="排序设置")
        group_sizer = wx.StaticBoxSizer(group_box, wx.VERTICAL)

        # 信号输入
        self.signal_ctrl = wx.TextCtrl(self)
        group_sizer.Add(
            self._create_row("排序信号:", self.signal_ctrl),
            0, wx.EXPAND | wx.BOTTOM, 5
        )

        # 参数行
        param_sizer = wx.BoxSizer(wx.HORIZONTAL)

        # 前K个
        self.topK_ctrl = wx.SpinCtrl(self, min=1, max=100)
        param_sizer.Add(
            self._create_row("取前K个:", self.topK_ctrl),
            1, wx.EXPAND | wx.RIGHT, 10
        )

        # 排除N个
        self.dropN_ctrl = wx.SpinCtrl(self, min=0, max=100)
        param_sizer.Add(
            self._create_row("排除后N个:", self.dropN_ctrl),
            1, wx.EXPAND | wx.RIGHT, 10
        )

        # 降序排序
        self.desc_check = wx.CheckBox(self, label="降序排序")
        param_sizer.Add(self.desc_check, 0, wx.ALIGN_CENTER_VERTICAL)

        group_sizer.Add(param_sizer, 0, wx.EXPAND)
        main_sizer.Add(group_sizer, 1, wx.EXPAND)

        self.SetSizer(main_sizer)

        # 初始化状态
        self._update_ui_state()
        self._bind_events()


    def _create_row(self, label, ctrl):
        """创建标签+控件的行布局"""
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(wx.StaticText(self, label=label), 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 5)
        sizer.Add(ctrl, 1, wx.EXPAND)
        return sizer

    def _bind_events(self):
        """事件绑定"""
        self.enable_check.Bind(wx.EVT_CHECKBOX, self._on_enable_changed)
        self.signal_ctrl.Bind(wx.EVT_TEXT, self._on_signal_changed)
        self.topK_ctrl.Bind(wx.EVT_SPINCTRL, self._on_config_change)
        self.dropN_ctrl.Bind(wx.EVT_SPINCTRL, self._on_config_change)
        self.desc_check.Bind(wx.EVT_CHECKBOX, self._on_config_change)

        #修改事件绑定
        self.Bind(wx.EVT_TEXT, self._notify_parent)
        self.Bind(wx.EVT_SPINCTRL, self._notify_parent)
        self.Bind(wx.EVT_CHECKBOX, self._notify_parent)

    def _notify_parent(self, event):
        """通知父窗口数据变化"""
        wx.PostEvent(self.GetParent(), wx.CommandEvent(wx.EVT_TEXT.typeId, self.GetId()))

    def _update_ui_state(self):
        """更新UI状态"""
        has_signal = bool(self.task.order_by_signal)
        enable_state = self.enable_check.GetValue()

        # 控制GroupBox状态
        self.signal_ctrl.Enable(enable_state)
        self.topK_ctrl.Enable(enable_state and has_signal)
        self.dropN_ctrl.Enable(enable_state and has_signal)
        self.desc_check.Enable(enable_state and has_signal)

        # 控制复选框初始状态
        self.enable_check.SetValue(has_signal)

    def _on_enable_changed(self, event):
        """启用状态变化"""
        if not self.prevent_update:
            self.task.order_by_signal = self.signal_ctrl.GetValue() if event.IsChecked() else ''
            self._update_ui_state()
            wx.PostEvent(self, wx.CommandEvent(wx.EVT_TEXT.typeId, self.GetId()))

    def _on_signal_changed(self, event):
        """信号输入变化"""
        if not self.prevent_update:
            self.task.order_by_signal = event.GetString()
            self._update_ui_state()
            wx.PostEvent(self, wx.CommandEvent(wx.EVT_TEXT.typeId, self.GetId()))

    def _on_config_change(self, event):
        """参数变化"""
        if not self.prevent_update:
            self.task.order_by_topK = self.topK_ctrl.GetValue()
            self.task.order_by_dropN = self.dropN_ctrl.GetValue()
            self.task.order_by_DESC = self.desc_check.GetValue()
            wx.PostEvent(self, wx.CommandEvent(wx.EVT_TEXT.typeId, self.GetId()))

    def update_controls(self):
        """从Task更新控件"""
        self.prevent_update = True
        try:
            self.signal_ctrl.SetValue(self.task.order_by_signal)
            self.topK_ctrl.SetValue(self.task.order_by_topK)
            self.dropN_ctrl.SetValue(self.task.order_by_dropN)
            self.desc_check.SetValue(self.task.order_by_DESC)
            self._update_ui_state()
        finally:
            self.prevent_update = False